package org.jeecg.modules.friends.service.impl;

import java.util.List;
import java.util.Objects;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.jeecg.modules.friends.auth.service.IUserAuthenticationService;
import org.jeecg.modules.friends.condition.InvitedUserCondition;
import org.jeecg.modules.friends.dao.InvitedUserDAO;
import org.jeecg.modules.friends.model.InvitedUserDO;
import org.jeecg.modules.friends.user.dto.InvitedUserInfo;
import org.jeecg.modules.friends.user.entity.User;
import org.jeecg.modules.friends.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InvitedUserServiceImpl implements org.jeecg.modules.friends.service.InvitedUserService {
    @Autowired
    private InvitedUserDAO invitedUserDAO;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IUserAuthenticationService userAuthenticationService;

    @Override
    public List<InvitedUserInfo> queryByUserId(Long userId) {
        List<InvitedUserDO> invitedUserDOS = invitedUserDAO.queryByUserId(userId);
        if (CollectionUtils.isNotEmpty(invitedUserDOS)) {
            List<InvitedUserInfo> invitedUserInfos = Lists.newArrayList();
            for (InvitedUserDO invitedUserDO : invitedUserDOS) {
                User user = userMapper.selectById(invitedUserDO.getInvitedUserId());
                if (Objects.isNull(user)) {
                    continue;
                }
                InvitedUserInfo invitedUserInfo = new InvitedUserInfo();
                invitedUserInfo.setUserId(invitedUserDO.getInvitedUserId());
                invitedUserInfo.setMobile(invitedUserDO.getMobile());
                invitedUserInfo.setInviteTime(invitedUserDO.getAddTime().getTime());
                invitedUserInfo.setNickName(user.getNickname());
                invitedUserInfo.setHeadPicUrl(user.getHeadPicUrl());
                invitedUserInfo.setAuthStatus(userAuthenticationService.queryUserAuthInfo(user).getAuthStatus());
                invitedUserInfo.setInviteUserVip(invitedUserDO.getInviteDayCount());
                invitedUserInfos.add(invitedUserInfo);
            }
            return invitedUserInfos;
        }
        return Lists.newArrayList();
    }

    @Override
    public InvitedUserDO queryByInvitedUserId(Long userId) {
        return invitedUserDAO.queryByInvitedUserId(userId);
    }
}